package com.fmm.medium;

import java.util.HashSet;
import java.util.Set;

public class FlipCard {

    /**
     * @param front 正面数字
     * @param back  背面数字
     * @return 任意翻转卡牌后，返回与所有正面数字都不同的最小数字
     */
    public static int flipCard(int[] front, int[] back) {
        Set<Integer> notMatchConditions = new HashSet<>();
        int min = Integer.MAX_VALUE;
        // 构建一定不可能是答案的数字集合（正反面数字都一样）
        for (int i = 0; i < front.length; i++) {
            if (front[i] == back[i]) {
                notMatchConditions.add(front[i]);
            }
        }
        for (int i = 0; i < front.length; i++) {
            // 符合条件的里面取最小值
            if (!notMatchConditions.contains(front[i])) {
                min = Math.min(min, front[i]);
            }
            if (!notMatchConditions.contains(back[i])) {
                min = Math.min(min, back[i]);
            }
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }

    public static void main(String[] args) {
        int[] front = new int[]{1, 2, 4, 4, 7};
        int[] back = new int[]{1, 3, 4, 1, 3};
        System.out.println(flipCard(front, back));

        front = new int[]{1};
        back = new int[]{1};
        System.out.println(flipCard(front, back));

        front = new int[]{2, 1, 1, 2, 2};
        back = new int[]{2, 2, 1, 2, 1};
        System.out.println(flipCard(front, back));
    }
}
